using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
    public class tbDonHangDAL
    {
        private SqlConnection cn = null;
        public tbDonHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public int Insert(tbDonHang donhang)
        {
            SqlCommand cm = new SqlCommand("sp_DonHang_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdKhachHang", donhang.IdKhachHang);
            cm.Parameters.AddWithValue("@NgayDatHang", donhang.NgayDatHang);
            cm.Parameters.AddWithValue("@IdTrangThaiDH", donhang.IdTrangThaiDH);
            cm.Parameters.AddWithValue("@NgayGiaoHang", donhang.NgayGiaoHang);
            cm.Parameters.AddWithValue("@DiaChiNhanHang", donhang.DiaChiNhanHang);
            return int.Parse(cm.ExecuteScalar().ToString());

        }

        public void Update(tbDonHang donhang)
        {
            SqlCommand cm = new SqlCommand("sp_DonHang_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdDH", donhang.IdDH );
            cm.Parameters.AddWithValue("@IdKhachHang", donhang.IdKhachHang);
            cm.Parameters.AddWithValue("@NgayDatHang", donhang.NgayDatHang);
            cm.Parameters.AddWithValue("@TriGia", donhang.TriGia );
            cm.Parameters.AddWithValue("@IdTrangThaiDH", donhang.IdTrangThaiDH);
            cm.Parameters.AddWithValue("@NgayGiaoHang", donhang.NgayGiaoHang);
            cm.Parameters.AddWithValue("@DiaChiNhanHang", donhang.DiaChiNhanHang);
            cm.ExecuteNonQuery();
        }

        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_DonHang_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdDH", Id);
            cm.ExecuteNonQuery();
        }

        public tbDonHangCollection  Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_DonHang_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbDonHangCollection donhangcollection = new tbDonHangCollection();
            while (dr.Read())
            {
                tbDonHang donhang = new tbDonHang();
                donhang.IdDH = int.Parse(dr.GetValue(0).ToString());
                donhang.IdKhachHang = int.Parse(dr.GetValue(1).ToString());
                donhang.NgayDatHang = DateTime.Parse(dr.GetValue(2).ToString());
                donhang.TriGia = int.Parse(dr.GetValue(3).ToString());
                donhang.IdTrangThaiDH = int.Parse(dr.GetValue(4).ToString());
                donhang.NgayGiaoHang = DateTime.Parse(dr.GetValue(5).ToString());
                donhang.DiaChiNhanHang = dr.GetValue(6).ToString();
                donhangcollection.Add(donhang);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return donhangcollection;
        }
    }
}
